package com.uphone.kingmall.activity.personal.set;

import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.blankj.utilcode.util.ToastUtils;
import com.china.zhuoyue.R;
import com.uphone.kingmall.base.BaseActivity;
import com.uphone.kingmall.utils.CommonUtil;
import com.uphone.kingmall.utils.FileUtil;
import com.uphone.kingmall.view.dialog.RadishDialog;

import butterknife.BindView;
import butterknife.OnClick;
//import io.rong.imageloader.utils.MemoryCacheUtils;

/**
 * <pre>
 *     author : 王雨
 *     e-mail : sunzengyan@outlook.com
 *     time   : 2018/07/31
 *     desc   :通用
 *     version: 1.0
 * </pre>
 */
public class TongYongActivity extends BaseActivity {
    @BindView(R.id.iv_back)
    ImageView ivBack;
    @BindView(R.id.rl_clear)
    RelativeLayout rlClear;
    @BindView(R.id.rl_notice)
    RelativeLayout rlNotice;
    @BindView(R.id.tv_cache)
    TextView tvCache;

    @Override
    public int getLayoutContentId() {
        return R.layout.kingmall_activity_tongyong;
    }

    @Override
    public void initView() {
        setStatusBar();
    }

    @Override
    public void initData() {

    }


    @OnClick({R.id.iv_back, R.id.rl_clear, R.id.rl_notice})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.iv_back:
                finish();
                break;
            case R.id.rl_clear:
                CommonUtil.showAlterDialog(this, "是否清空缓存", new CommonUtil.OnAlterDialogCallBack() {
                    @Override
                    public void callBack(RadishDialog dialog, View view) {

                        clearAppCache();
                    }
                });
                break;
            case R.id.rl_notice:
                openActivity(MessageNoticeSetActivity.class);
                break;
        }
    }

    /**
     * 清除app缓存
     *
     * @param
     */
    private final int CLEAN_SUC = 1001;
    private final int CLEAN_FAIL = 1002;

    public void clearAppCache() {
        new Thread() {
            @Override
            public void run() {
                Message msg = Message.obtain();
                try {
                    myclearaAppCache();
                    msg.what = CLEAN_SUC;
                } catch (Exception e) {
                    e.printStackTrace();
                    msg.what = CLEAN_FAIL;
                }
                handler.sendMessage(msg);
            }
        }.start();
    }

    private Handler handler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            switch (msg.what) {
                case CLEAN_FAIL:
                    ToastUtils.showShort("清除失败");
                    break;
                case CLEAN_SUC:
                    ToastUtils.showShort("清除成功");
                    break;
            }
        }
    };

    /**
     * 清除app缓存
     */
    public void myclearaAppCache() {
        FileUtil.deleteFilesByDirectory(getCacheDir());
        FileUtil.deleteFilesByDirectory(getFilesDir());
        FileUtil.deleteFilesByDirectory(getExternalCacheDir());
        FileUtil.deleteFilesByDirectory(getExternalFilesDir(""));
    }
}
